'use strict'

define(['jquery', 'bootstrap', 'select2', 'datetimepicker', 'datetimepickerCN',
        'shiftform', 'validate', 'validate.en'], function($) {
  
  return {
        
    init: function(data) {

        this.initPage(data.status);
        this.initRefund();
        this.initChangeSalesman();
        this.initPay();
    },
    
    initPage: function(status){
    
        $('select[name="siteId"]').select2({
            minimumResultsForSearch : -1,
            allowClear : true,
            width: '150'
          });
    
          $('select[name="siteId"]').change(function(){ 
            var site = $(this).children('option:selected').val();
            window.location.href = window.__CTX__.root + "/group-orders/?siteId=" + site + "&status=" + status;
          });
    },
    
    initRefund: function(){
        $(document).on("shown.bs.modal", "#refund", function(e) {
        var $form = $(e.currentTarget).find("form");
      
        $form.bt_validate();
      
        $form.shiftform({
            beforeSubmit: function(el) {
              $('form').trigger("validate.ajax.submit");
                   return $.bt_validate.result[$.bt_validate.form_id]
            }
        });
        
        })
        
    },
    
    initChangeSalesman: function(){
        $(document).on("shown.bs.modal", "#change-salesman", function(e) {
        var $form = $(e.currentTarget).find("form");
      
        $form.bt_validate();
      
        $form.shiftform({
            beforeSubmit: function(el) {
              $('form').trigger("validate.ajax.submit");
                   return $.bt_validate.result[$.bt_validate.form_id]
            }
        });
        
        })
        
    },
    
    initPay: function(){
        $(document).on("shown.bs.modal", "#pay", function(e) {
        var $form = $(e.currentTarget).find("form");
      
        $form.bt_validate();
      
        $form.shiftform({
            beforeSubmit: function(el) {
              $('form').trigger("validate.ajax.submit");
                   return $.bt_validate.result[$.bt_validate.form_id]
            }
        });
        
        })
        
    }
    
   }
})
